package cn.momoshenchi.ware.service.impl;

import cn.momoshenchi.common.constant.WareConstant;
import cn.momoshenchi.ware.entity.PurchaseDetailEntity;
import cn.momoshenchi.ware.service.PurchaseDetailService;
import cn.momoshenchi.ware.service.WareSkuService;
import cn.momoshenchi.ware.vo.MergeVo;
import cn.momoshenchi.ware.vo.PurchaseDoneVo;
import cn.momoshenchi.ware.vo.PurchaseItemDoneVo;
import com.baomidou.mybatisplus.annotation.TableField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.common.utils.PageUtils;
import cn.momoshenchi.common.utils.Query;

import cn.momoshenchi.ware.dao.PurchaseDao;
import cn.momoshenchi.ware.entity.PurchaseEntity;
import cn.momoshenchi.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params)
    {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);


    }
    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo)
    {
        Long purchaseId = mergeVo.getPurchaseId();
        if(purchaseId==null)
        {
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            save(purchaseEntity);
            purchaseId=purchaseEntity.getId();
        }
        List<Long>items=mergeVo.getItems();
        Long finalPurchaseId=purchaseId;
        List<PurchaseDetailEntity> purchaseDetailEntities = items.stream().map(i -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setId(i);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseStatusEnum.ASSIGNED.getCode());
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(purchaseDetailEntities);
    }

    /**
     *
     *
     * @param ids 采购单id
     */
    @Override
    public void received(List<Long> ids)
    {
        //1.确认采购单的状态
        QueryWrapper <PurchaseEntity>wrapper=new QueryWrapper<>();
        List<PurchaseEntity> purchaseEntitiestemp = list(wrapper);
        List<PurchaseEntity> purchaseEntities = purchaseEntitiestemp.stream().filter(item -> {
            return item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
                    item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode();
        }).peek(item -> item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVED.getCode()))
                .collect(Collectors.toList());
        //2. 改变单状态
        updateBatchById(purchaseEntities);

        //3.改变需求状态
        purchaseEntities.forEach(item->{
            QueryWrapper<PurchaseDetailEntity>queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("purchase_id",item.getId());
            List<PurchaseDetailEntity> purchaseDetailEntitiestemp = purchaseDetailService.list(queryWrapper);
            List<PurchaseDetailEntity> purchaseDetailEntities = purchaseDetailEntitiestemp.stream()
                    .peek(i -> i.setStatus(WareConstant.PurchaseStatusEnum.RECEIVED.getCode()))
                    .collect(Collectors.toList());
            purchaseDetailService.updateBatchById(purchaseDetailEntities);
        });


    }
@Transactional
    @Override
    public void done(PurchaseDoneVo purchaseDoneVo)
    {

        boolean flag=true;
        List<PurchaseItemDoneVo> purchaseDoneVoItems = purchaseDoneVo.getItems();

        //1.改变采购需求状态
        List<PurchaseDetailEntity>updates =new ArrayList<>();
        for (PurchaseItemDoneVo purchaseDoneVoItem : purchaseDoneVoItems)
        {
            PurchaseDetailEntity purchaseDetailEntity=new PurchaseDetailEntity();
            if(purchaseDoneVoItem.getStatus()==WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode())
            {
                flag=false;
                purchaseDetailEntity.setStatus(purchaseDoneVoItem.getStatus());
            }
            else{
                //2. 修改库存数量
                purchaseDetailEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISHED.getCode());
                PurchaseDetailEntity purchaseDetailEntity1 = purchaseDetailService.getById(purchaseDoneVoItem.getItemId());
                wareSkuService.addStock(purchaseDetailEntity1);
            }
            purchaseDetailEntity.setId(purchaseDoneVoItem.getItemId());
            updates.add(purchaseDetailEntity);
        }
        purchaseDetailService.updateBatchById(updates);
        //3.更新采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseDoneVo.getId());
        purchaseEntity.setStatus(flag?WareConstant.PurchaseStatusEnum.FINISHED.getCode() :
                WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        updateById(purchaseEntity);


    }

}